package com.zp.qualitypractice.fragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;

import android.support.v7.app.AppCompatActivity;

import com.zp.qualitypractice.R;

/**
 * Created by izp on 2017/6/26.
 */

public class SampleActivity extends AppCompatActivity {
    private LazyFragment2 oneFragment;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(android.R.layout.list_content);
        FragmentManager fm = getFragmentManager();
        DemoOfABase fragment = (DemoOfABase) fm.findFragmentByTag(DemoOfABase.class.getSimpleName());

        if(fragment == null ) {
            fragment = DemoOfABase.newInstance(DemoOfABase.class.getSimpleName()) ;
            fm.beginTransaction().add(-1,fragment).commit();
        }
    }

    /**
     * 避免activity中的Fragment重复创建的方法
     * 1/
     */
    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if (fragment instanceof  LazyFragment2){
            oneFragment = (LazyFragment2) fragment;
        }
    }
    /**
     * 避免activity中的Fragment重复创建的方法
     * 2/
     */
    private void aVoidRepeatFragment(FragmentTransaction ft){
        Fragment tempFragment = getFragmentManager().findFragmentByTag("OneFragment");
        if (tempFragment==null) {
            oneFragment =  new LazyFragment2();
            ft.add(R.id.frame_layout_content_place, oneFragment, "OneFragment");
        }else {
            oneFragment = (LazyFragment2) tempFragment;
        }
    }
    /**
     * 避免activity中的Fragment重复创建的方法
     * 3/
     */
    private void aVoidRepeatFragment(FragmentTransaction ft,Bundle savedInstanceState){
        if (savedInstanceState==null) {
            oneFragment = new LazyFragment2();
            ft.add(R.id.frame_layout_content_place, oneFragment, "OneFragment");
        }else {
            oneFragment = (LazyFragment2) getFragmentManager().findFragmentByTag("OneFragment");
        }
    }
}
